<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Eproc extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
	}
	public function index()
	{
		if($this->auth->is_logged_in() == false)
		{
			$this->login();
		}
		else
		{
			$this->template->set('title','Selamat Datang di EPROC PTPN4');
			$this->template->load('template','admin/index');
		}
	}
	public function manajemen_menu()
	{
		// mencegah user yang belum login untuk mengakses halaman ini
		$this->auth->restrict();
		// mencegah user mengakses menu yang tidak boleh dibuka
		$this->auth->cek_menu(1);
		// tampilkan isi menu manajemen menu (mungkin tabel menu/input form menu)
		$this->template->set('title','Manajemen User | EPROC PTPN4');
		$this->template->load('template','admin/manajemen_menu');
	}
	public function login()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_rules('username', 'Username', 'trim|required');
		$this->form_validation->set_rules('password', 'Password', 'trim|required');
		$this->form_validation->set_error_delimiters(' <span style="color:#FF0000">', '</span>');

		if ($this->form_validation->run() == FALSE)
		{
			$this->template->set('title','Login Admin | EPROC PTPN4');
			$this->template->load('template','admin/login_form');
		}
		else
		{
			$username = $this->input->post('username');
			$password = $this->input->post('password');
			$success = $this->auth->do_login($username,$password);
			if($success)
			{
				// lemparkan ke halaman index user
				redirect('eproc/index');
			}
			else
			{
				$this->template->set('title','Login | EPROC PTPN4');
				$data['login_info'] = "Maaf, username atau password anda salah!";
				$this->template->load('template','admin/login_form',$data);		
			}
		}
	}
	function logout()
	{
		if($this->auth->is_logged_in() == true)
		{
			// jika dia memang sudah login, destroy session
			$this->auth->do_logout();
		}
		// larikan ke halaman utama
		redirect('eproc');
	}
	
	function manajemen_user()
	{
		// mencegah user yang belum login untuk mengakses halaman ini
		$this->auth->restrict();
		// mencegah user mengakses menu yang tidak boleh ia buka (manejemen user menu_id = 2)
		$this->auth->cek_menu(2);
    
		// get data dari usermodel
		$data['all_user'] = $this->usermodel->get_all_user();
		$this->template->set('title','Manajemen User | EPROC PTPN4');
		// load view tabel_user.php di folder application/views/admin/
		$this->template->load('template','admin/tabel_user',$data);
	}
	function insert_user()
	{
		// mencegah user yang belum login untuk mengakses halaman ini
		$this->auth->restrict();
		// mencegah user mengakses menu yang tidak boleh ia buka (manejemen user menu_id = 2)
		$this->auth->cek_menu(2);
		
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('nama', 'Nama', 'trim|required');
		$this->form_validation->set_rules('username', 'Username', 'trim|required');
		$this->form_validation->set_rules('password', 'Password', 'trim|required');
		$this->form_validation->set_rules('password_conf', 'Konfirmasi Password', 'trim|required|matches[password]');
		$this->form_validation->set_rules('level', 'Level', 'trim|required');
		$this->form_validation->set_rules('urusan', 'Urusan', 'trim|required');
		
		$this->form_validation->set_error_delimiters('<span style="color:#FF0000">', '</span>');

		if ($this->form_validation->run() == FALSE)
		{
			$data['level'] = $this->usermodel->get_all_level();
			$this->template->set('title','Tambah User Baru | EPROC PTPN4');
			$this->template->load('template','admin/form_input_user',$data);
		}
		else
		{
			$data_user = array(
				'nama'	=>$this->input->post('nama'),
				'username'	=>$this->input->post('username'),
				'password'	=>md5($this->input->post('password')),
				'user_level'	=>$this->input->post('level')
			);
			$this->usermodel->insert_data_user($data_user);
			// kembalikan ke halaman manajemen user
			redirect('eproc/manajemen_user');		
		}
	}
	function edit_user()
	{
		// mencegah user yang belum login untuk mengakses halaman ini
		$this->auth->restrict();
		// mencegah user mengakses menu yang tidak boleh ia buka (manejemen user menu_id = 2)
		$this->auth->cek_menu(2);
		
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('nama', 'nama', 'trim|required');
		$this->form_validation->set_rules('username', 'Username', 'trim|required');
		$this->form_validation->set_rules('password', 'Password', 'trim|required');
		$this->form_validation->set_rules('password_conf', 'Password Confirmation', 'trim|required|matches[password]');
		$this->form_validation->set_rules('level', 'Level', 'trim|required');
		
		$this->form_validation->set_error_delimiters(' <span style="color:#FF0000">', '</span>');
		
		// dapatkan id user dari segment ke-3 dari URI
		$id = $this->uri->segment(3);
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['level'] = $this->usermodel->get_all_level();
			// dapatkan data user yg akan diedit dari database
			$data['user'] = $this->usermodel->get_user_by_id($id);
			$this->template->set('title','Edit User | MyWebApplication.com');
			$this->template->load('template','admin/form_edit_user',$data);
		}
		else
		{
			$data_user = array(
				'user_nama'	=>$this->input->post('nama'),
				'user_username'	=>$this->input->post('username'),
				'user_password'	=>md5($this->input->post('password')),
				'user_level'	=>$this->input->post('level')
			);
			$this->usermodel->update_data_user($data_user,$id);
			// kembalikan ke halaman manajemen user
			redirect('eproc/manajemen_user');		
		}
	}
	function delete_user()
	{
		// mencegah user yang belum login untuk mengakses halaman ini
		$this->auth->restrict();
		// mencegah user mengakses menu yang tidak boleh ia buka (manejemen user menu_id = 2)
		$this->auth->cek_menu(2);
		// dapatkan id user dari segment ke-3 dari URI
		$id = $this->uri->segment(3);
		$this->usermodel->delete_user($id);
		// kembalikan ke halaman manajemen user
		redirect('eproc/manajemen_user');	
	}
	function hps()
	{
		// mencegah user yang belum login untuk mengakses halaman ini
		$this->auth->restrict();
		// mencegah user mengakses menu yang tidak boleh ia buka (manejemen user menu_id = 2)
		$this->auth->cek_menu(2);
		// ambil data dari eprocmodel
		$data['all_hps'] = $this->eprocmodel->get_all_hps();
		$this->template->set('title','Manajemen HPS | EPROC PTPN4');
		// load view tabel_user.php di folder application/views/eproc/
		$this->template->load('template','eproc/tabel_hps',$data);
	}
	function detil_hps()
	{	//Fungsi untuk menampilkan detil informasi terkait dengan HPS yang dipilih
		// mencegah user yang belum login untuk mengakses halaman ini
		$this->auth->restrict();
		// mencegah user mengakses menu yang tidak boleh ia buka (manejemen user menu_id = 2)
		$this->auth->cek_menu(2);
		// ambil data dari eprocmodel
		$data['all_hps'] = $this->eprocmodel->get_hps_by_id($id);
		$this->template->set('title','Manajemen HPS | EPROC PTPN4');
		// load view tabel_user.php di folder application/views/eproc/
		$this->template->load('template','eproc/tabel_hps',$data);
	}
	function insert_hps()
	{
		// mencegah user yang belum login untuk mengakses halaman ini
		$this->auth->restrict();
		// mencegah user mengakses menu yang tidak boleh ia buka (manejemen user menu_id = 2)
		$this->auth->cek_menu(2);
		
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('no_hps', 'Nomor HPS', 'trim|required');
		$this->form_validation->set_rules('tgl_hps', 'Tanggal HPS', 'trim|required|date');
		$this->form_validation->set_rules('objek_pekerjaan', 'Objek Pekerjaan', 'trim|required');
		$this->form_validation->set_rules('rincian_hps', 'File Rincian HPS', 'trim|required');
		$this->form_validation->set_rules('nilai_hps', 'Nilai HPS', 'trim|required|integer');
		$this->form_validation->set_error_delimiters(' <span style="color:#FF0000">', '</span>');

		if ($this->form_validation->run() == FALSE)
		{
			$data['level'] = $this->usermodel->get_all_level();
			$this->template->set('title','Tambah HPS Baru | EPROC PTPN4');
			$this->template->load('template','eproc/form_input_hps',$data);
		}
		else
		{
			$data_hps = array(
				'no_hps'	=>$this->input->post('no_hps'),
				'tgl_hps'	=>$this->input->post('tgl_hps'),
				'objek_pekerjaan'	=>$this->input->post('objek_pekerjaan'),
				'rincian_hps'	=>$this->input->post('rincian_hps'),
				'nilai_hps'	=>$this->input->post('nilai_hps'),
				'id_rekam'	=>$this->session->userdata('user_id')				
			);
			$this->eprocmodel->insert_data_hps($data_hps);
			// kembalikan ke halaman manajemen user
			redirect('eproc/manajemen_hps');		
		}
	}
}